function out_img(name, url) {
	url += '?imageView2/2/w/322';
	return '<img src="' + url + '" alt="' + name + '" width="100%"/>';
}

function out_head(head, url) {
	return '<a target="_blank" class="wd_head" href="' + url + '">' + head + '</a>';
}

function out_url(val, url) {
	return '<a target="_blank" class="btn btn-info" href="' + url + '">' + val + '</a>';
}

function out_intro(str) {
	var strong_reg = /\*\*([^\*]*)\*\*/g;
	str = str.replace(strong_reg, '<strong>$1</strong>');

	var em_reg = /\*([^\*]*)\*/g;
	str = str.replace(em_reg, '<em>$1</em>');

	var code_reg = /`(.*)`/g;
	str = str.replace(code_reg, '<code>$1</code>');

	var res = '<p>';
	res += str;
	res += '</p>';
	return res;
}

function out_html(resObj) {
	var res = '<div class="wd_detail line" wid="' + resObj.wid + '"><div>';
	res += '<div class="col-md-3">';
	res += '<a target="_blank" href="' + resObj.head_url + '">' + resObj.img + '</a>';
	res += '</div><div class="col-md-9">' + resObj.head;
	res += '<div class="wd_intro_list">';
	for (x in resObj.intro_arr) {
		res += resObj.intro_arr[x];
	}
	res += '</div><div class="wd_url_list">';
	for (x in resObj.url_arr) {
		res += resObj.url_arr[x];
	}
	res += '</div></div><div style="clear:both;"></div></div>';
	res += '<div class="row"><div class="col-md-9"></div><div class="col-md-1"><a href="javascript:void(0)" class="delete_wd btn btn-link">删除</a></div><div class="col-md-2"><a href="#" class="edit_wd btn btn-link">编辑</a></div></div></div>';
	return res;
}

function input_parse(input_str, wid) {
		if (input_str.length < 3) {
			$("#wd_detail_demo").html('');
			return;
		}
		input_arr = input_str.split("\n");
		var resObj = {};
		resObj.url_arr = new Array();
		resObj.intro_arr = new Array();
		resObj.head = '';
		resObj.head_url = '#';
		resObj.img = '';
		resObj.wid = wid;

		for (i in input_arr) {
			str = input_arr[i];
			if(head_reg.test(str)) {
				arr = str.match(head_reg);
				resObj.head = out_head(arr[1], arr[2]);
				resObj.head_url = arr[2];
			} else if (img_reg.test(str)) {
				arr = str.match(img_reg);
				resObj.img = out_img(arr[1], arr[2]);
			} else if (url_reg.test(str)) {	
				arr = str.match(url_reg);
				resObj.url_arr.push(out_url(arr[1], arr[2]));
			} else {
				resObj.intro_arr.push(out_intro(str));
			}
		}
		return out_html(resObj);
}
function textarea_add_source_link(source_link) {
	var textarea = $("#input textarea");
	var link_str = '![](' + source_link + ')\n' + textarea.val();
	textarea.val(link_str);
}

function timedCount(){
	setTimeout("timedCount()",2000);
	$("#wd_detail_demo").html(input_parse($("#input textarea").val(), ""));
}

function init() {
	init_wds_list();
	timedCount();
	$("#wd_input_box").hide();
}

function upload_progress(percent) {
	if (percent == 0) {
		$("#line_progress_bar").animate({width: "100%"}, "fast", function() {
			$("#line_progress_bar").width(0);
		});
		return;
	}
	percent += '%';
	$("#line_progress_bar").animate({width: percent}, "fast");
}

function init_wds_list() {
	request = $.ajax({
		url: "/z/userwds/",
		type: "GET",
		dataType: "json",
	});
	request.done(function(data){
		switch(data.ret) {
			case 0: {
				for (wd in data.wds) {
					var res = '<li><a href="javascript:void(0);">' + data.wds[wd] + '</a></li>';
					$("#wd_list").prepend(res);
				}
				break;
			}
			case 2001: {
				alert("数据库错误 2001")
				break;
			}
		}
	});
}

$(document).ready(function(){
	init();
	img_reg = /!\[(.*)\]\((.*)\)/;
	head_reg = /#(.*)\((.*)\)/;
	url_reg = /\[(.*)\]\[(.*)\]/;

	var current_wd_li;

	$("#publish_btn").click(function(){

		input_str = $("#input textarea").val();

		if (input_str.length < 4) {
			return;
		};

		if (typeof current_wd_li == "undefined") {
			alert("请先选中关键词");
			return;
		};

		$("#wd_detail_real").prepend(input_parse($("#input textarea").val(), ""));
		$("#wd_detail_demo").empty();
		$("#input textarea").val("");

		var wd = current_wd_li.children().html();

		post_data = {
			wd: wd,
			wdContent: input_str,
		};

		request = $.ajax({
			url: "/z/addwd/",
			type: "POST",
			data: post_data,
			dataType: "json"
		});

		request.done(function(data) {
			switch (data.ret) {
				case 0: {
					wd_detail = $("#wd_detail_real div").first();
					wd_detail.attr("wid", data.wid);
					alert("发布成功");
					break;
				}

				case 2000: {
					alert("数据库错误 2000")
					break;
				}
			}
		});
	});

	$("#search_form").submit(function(event) {
		// 搜索
		event.preventDefault();
		var search_wd = $("#search_wd").val();

		wd_detail_real = $("#wd_detail_real");
		wd_detail_real.empty()

		post_data = {
			wd: search_wd,
		};

		request = $.ajax({
			url: "/z/search/",
			type: "POST",
			data: post_data,
			dataType: "json"
		});

		request.done(function(data){
			for (x in data) {
				wid = data[x]._id;
				wdContent = data[x].wdContent;
				$("#wd_detail_real").prepend(input_parse(wdContent, wid));
			}
		});

	});
	$("#add_wd").click(function() {
		$("#wd_input_box").show();
		if (typeof current_wd_li != 'undefined') {
			current_wd_li.removeClass();
		};
	});
	$(document).on("click", "a.delete_wd", function() {
		var sure = confirm("你确定删除该条内容吗?");
		if (sure) {
			$(this).parent().parent().parent().remove();
		}
	});

	$(document).on("click", "a.edit_wd", function() {
		alert("OK");
	});

	$(document).on("click", "#wd_list li a", function() {
		var wd = $(this).html();
		if (typeof current_wd_li != 'undefined') {
			current_wd_li.removeClass();
		};
		current_wd_li = $(this).parent();
		current_wd_li.addClass("active");

		wd_detail_real = $("#wd_detail_real");
		wd_detail_real.empty()

		post_data = {
			wd: wd,
		};

		request = $.ajax({
			url: "/z/detail/",
			type: "POST",
			data: post_data,
			dataType: "json"
		});

		request.done(function(data){
			for (x in data) {
				wid = data[x]._id;
				wdContent = data[x].wdContent;
				$("#wd_detail_real").prepend(input_parse(wdContent, wid));
			}
		});
	});

	$("#wd_input_form").submit(function() {
		// 将新增关键字设为active并移除上个关键字的active属性
		if (typeof current_wd_li != 'undefined') {
			current_wd_li.removeClass();
		};

		event.preventDefault();
		var wd_input = $("#new_wd_input");
		var wd = wd_input.val();
		wd_input.val("");
		if (wd.length > 0) {
			var res = '<li class="active"><a href="javascript:void(0);">' + wd + '</a></li>';
			$("#wd_list").prepend(res);
			current_wd_li = $("#wd_list li:first");
		};
		$("#wd_input_box").hide();
	});
});
























